$import("com.joypard.platform.account.user.UserDI");
/**
 * 用户
 */
K.defView("com.joypard.platform.account.user.UserView", {
	userCtrl : null,
	purviewConfig : {
		view : "platform.account.user",
		addUser : "platform.account.user.addUser",
		updateUser : "platform.account.user.updateUser",
		delUser : "platform.account.user.delUser"
	},
	initialize : function() {
		E.addListener("updateUserSuccessEvent", this.onUpdateUserSuccess, this);
		E.addListener("addUserSuccessEvent", this.onAddUserSuccess, this);
		E.addListener("delUserSuccessEvent", this.onDelUserSuccess, this);
	},
	render : function() {
		this.userSearchPanel = this.createUserSearchPanel(); // 条件查询面板
		this.userGridPanel = this.createUserGridPanel(); // 用户列表
		this.userEditPanel = this.createUserEditPanel();

		this.rolePanel = this.createRolePanel(); // 角色面板

		this.westUserPanel = this.createWestUserPanel(); // 左侧用户面板
		this.westUserPanel.add(this.userSearchPanel);
		this.westUserPanel.add(this.userGridPanel);

		this.centerTabPanel = this.createCenterTabPanel(); // 中间标签页面板
		this.centerTabPanel.add(this.userEditPanel);
		this.centerTabPanel.add(this.rolePanel);

		this.userGridPanel.getStore().on("load", function() {
			if (this.getSelectionModel().getCount() == 0) {
				this.getSelectionModel().selectFirstRow();
			}
		}, this.userGridPanel);
		this.userGridPanel.on("afterrender", function() {
			this.getStore().load();
		}, this.userGridPanel);
		this.userGridPanel.getSelectionModel().on("rowselect", this.onRowselect, this);

		this.MP = new K.Ext.MainPanel({
			items : [ this.westUserPanel, this.centerTabPanel ]
		});
	},
	/**
	 * 左侧面板
	 */
	createWestUserPanel : function() {
		var options = {
			layout : "border",
			border : false,
			margins : "2 2 2 2",
			width : "60%",
			region : "west"
		};
		return new K.Ext.Panel(options);
	},
	/**
	 * 创建用户搜索面板
	 */
	createUserSearchPanel : function() {
		var options = {
			title : "条件查询",
			frame : true,
			region : "north",
			margins : "0 0 2 0",
			iconCls : "p-search",
			collapsible : true,
			bodyCssCls : "search-gb",
			contentEl : "user-search"
		};
		var me = this;
		$("#user-search").keypress(function(evt) {
			var tagName = evt.target.tagName;
			if (tagName == "INPUT" && evt.keyCode == Ext.EventObject.ENTER) {
				me.onSearchUser();
			}
		});
		return new K.Ext.Panel(options);
	},
	/**
	 * 创建用户列表面板
	 */
	createUserGridPanel : function() {
		var sm = new K.Ext.grid.CheckboxSelectionModel(), options = {
			title : "用户列表",
			iconCls : "p-grid",
			region : "center",
			tbar : [ {
				text : "新增",
				iconCls : 'add',
				hidden : !UserView.purviewAddUser(),
				handler : K.createDelegate(this.onAddUser, this)
			}, {
				text : "删除",
				iconCls : 'delete',
				hidden : !UserView.purviewDelUser(),
				handler : K.createDelegate(this.onDelUser, this)
			}, {
				text : "查询",
				iconCls : 'search',
				id : "searchUserBtn",
				handler : K.createDelegate(this.onSearchUser, this)
			} ],
			store : UserDI.createGetUsersStore(),
			sm : sm,
			columns : [ sm, {
				header : "用户编码",
				dataIndex : "userCode"
			}, {
				header : "姓名",
				dataIndex : "userName"
			}, {
				header : "账号",
				dataIndex : "account"
			}, {
				header : "昵称",
				dataIndex : "userNickName"
			}, {
				header : "邮箱",
				dataIndex : "email"
			}, {
				header : "手机",
				dataIndex : "mobile"
			} ],
			bbar : {
				xtype : "pagination"
			}
		};
		return new K.Ext.grid.GridPanel(options);
	},

	/**
	 * 创建右侧面板
	 */
	createCenterTabPanel : function() {
		var options = {
			region : "center",
			border : false,
			margins : "2 2 2 0",
			layoutConfig : {},
			layout : "vbox"
		};
		return new K.Ext.Panel(options);
	},

	/**
	 * 创建用户编辑面板
	 */
	createUserEditPanel : function() {
		var options = {
			title : "用户信息",
			iconCls : "p-edit",
			width : "100%",
			tbar : {
				xtype : "toolbar",
				hidden : !UserView.purviewUpdateUser() && !UserView.purviewAddUser(),
				items : [ {
					text : "保存",
					iconCls : "save",
					handler : K.createDelegate(this.onSaveUser, this)
				} ]
			}
		};
		return new K.Ext.Panel(options);
	},
	/**
	 * 创建角色面板
	 */
	createRolePanel : function() {
		options = {
			title : "角色信息",
			iconCls : "p-grid",
			margins : "2 0 0 0",
			flex : 1,
			store : UserDI.createGetUserRolesStore(),
			viewConfig : {
				forceFit : true
			},
			tbar : [ {
				xtype : "label",
				text : "用户："
			}, {
				xtype : "label",
				id : "curUserLabel",
				text : ""
			}, {
				text : "",
				hidden : true,
				hideMode : "visibility"
			} ],
			columns : [ {
				header : "所在系统",
				renderer : function(value, meta, record) {
					var app = record.get("appSystem");
					return app ? app.appName : "";
				}
			}, {
				header : "角色编码",
				dataIndex : "code"
			}, {
				header : "角色名称",
				dataIndex : "name"
			} ]
		};
		return new K.Ext.grid.GridPanel(options);
	},

	// 事件定义
	/**
	 * 单击用户列表行
	 */
	onRowselect : function(selModel, rowIndex, record) {
		var me = this;
		this.userEditPanel.setTitle("用户信息");
		this.userEditPanel.load({
			url : UserDI.urls.updateUser,
			method : "GET",
			callback : function() {
				me.centerTabPanel.doLayout();
			},
			params : {
				userId : record.id
			}
		});

		this.rolePanel.getTopToolbar().get("curUserLabel").setText(record.get("userName"));
		this.rolePanel.getStore().load({
			params : {
				userId : record.id
			}
		});
	},
	/**
	 * 单击新增按钮
	 */
	onAddUser : function() {
		this.userEditPanel.setTitle("新增用户信息");
		this.userEditPanel.load({
			url : UserDI.urls.addUser,
			method : "GET"
		});
	},
	/**
	 * 删除用户
	 */
	onDelUser : function() {
		var records = this.userGridPanel.getSelectionModel().getSelections();
		if (records.length > 0) {
			var msg = [ "删除的用户数量为:{num}" ];
			msg.push("---------------------------------------------------------------------------");
			msg.push(K.arrayToStr(records, function(r) {
				return r.get("userName");
			}, ", "));
			msg.push("---------------------------------------------------------------------------");
			msg.push("您确定要删除吗?");
			msg = K.format(msg.join("\n"), {
				num : records.length
			});
			if (!confirm(msg)) {
				return;
			}
			E.fireEvent("delUserEvent", records);
		} else {
			K.err("请选择用户", this.userGridPanel.tbar);
		}
	},
	/**
	 * 点击查询用户按钮
	 */
	onSearchUser : function() {
		this.userGridPanel.getStore().load({
			params : K.serializeJson("#user-search")
		});
	},
	/**
	 * 点击保存用户信息按钮
	 */
	onSaveUser : function() {
		var form = $("#userForm"), state = form.attr("state"), eventName = null;
		if ("add" == state) {
			if (UserView.purviewAddUser()) {
				eventName = "addUserEvent";
			} else {
				K.noright(this.userEditPanel.tbar);
			}
		} else if ("update" == state) {
			if (UserView.purviewUpdateUser()) {
				eventName = "updateUserEvent";
			} else {
				K.noright(this.userEditPanel.tbar);
			}
		}

		if (eventName) {
			E.fireEvent(eventName, form);
		}
	},
	onUpdateUserSuccess : function(output) {
		var user = output.getParameter("user"), store = this.userGridPanel.getStore();
		var record = store.getById(user.id);
		K.Ext.flyRecord(record).set(user).commit();
		K.m("修改成功").show(this.userEditPanel.tbar);
	},
	onAddUserSuccess : function() {
		K.m("新增成功").show(this.userEditPanel.tbar);
		this.userGridPanel.getStore().reload();
	},
	onDelUserSuccess : function(output) {
		K.m("删除成功").show(this.userGridPanel.tbar);
		this.userGridPanel.getStore().reload();
	}
});
